/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sk.lieskove.jianghongtiao.paris.webclient.web;

import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.integration.spring.SpringBean;
import org.apache.commons.io.FileUtils;
import sk.lieskove.jianghongtiao.websearch.enums.DocumentType;
import sk.lieskove.jianghongtiao.paris.webclient.model.DocumentManager;
import sk.lieskove.jianghongtiao.paris.webclient.persistence.ClassificationItem;
import sk.lieskove.jianghongtiao.websearch.document.preprocessing.SupportedLanguages;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

/**
 *
 * @author xjuraj
 */
@UrlBinding(value = "/classification/{$event}/{csId}")
public class ClassificationActionBean extends AbstractActionBean {

    @SpringBean
    private DocumentManager documentManager;
    private List<ClassificationItem> classificationItems;
    private ClassificationItem classificationItem;
    private String extractedText = "";
    private Long csId;
    private Long wsdId;
    private DocumentType documentType;
    private SupportedLanguages languageName;

    @DefaultHandler
    public Resolution notClassified() {
        log.debug("Classification AB -> NotClassified");
        setClassificationItem(documentManager.getFirstNotProcessed());
        if (classificationItem == null) {
            return new ForwardResolution("/WEB-INF/pages/thatIsAll.jsp");
        }
        loadExtractedText();
        this.getContext().getRequest().setAttribute("documentTypeEnum", 
                DocumentType.values());
        this.getContext().getRequest().setAttribute("numberRemaining", 
                documentManager.getNumberOfRemaining());
        return new ForwardResolution("/WEB-INF/pages/classification.jsp");
    }
    
    public Resolution item() {
        log.debug("Classification AB -> Item");
//        Long csId = getCsId();
        if (csId == null) {
            return notClassified();
        } else {
            setClassificationItem(documentManager.getItem(csId));
            if (classificationItem == null) {
                return new ForwardResolution("/WEB-INF/pages/notFound.jsp");
            }
            loadExtractedText();
            return new ForwardResolution("/WEB-INF/pages/classification.jsp");
        }
    }

    public Resolution getOriginalDocument() {
        log.debug("Classification AB -> getOriginalDocument");
//        Long csId = getCsId();
        if (csId == null) {
            return new ForwardResolution("/WEB-INF/pages/notFound.jsp");
        } else {
            setClassificationItem(documentManager.getItem(csId));
            if (classificationItem == null) {
                return new ForwardResolution("/WEB-INF/pages/notFound.jsp");
            }
            StreamingResolution sr = null;
            try {
                File original = new File(classificationItem.getOriginalFileLocation());
                sr = new StreamingResolution(classificationItem.getMimeType(),
                        new FileInputStream(original));
                sr.setCharacterEncoding(classificationItem.getEncoding());
                sr.setLength(original.length());
            } catch (FileNotFoundException ex) {
                log.error("Original document was not found: "
                        + classificationItem.getOriginalFileLocation());
            }
            return sr;
        }
    }
    
    public Resolution update(){
        log.debug("Classification AB -> Update");
//        Long csId = getCsId();
//        Long wsdId = getId("wsdId");
//        DocumentType documentType = DocumentType.
//                valueOf(getContext().getRequest().getParameter("documentType"));
//        SupportedLanguages language = SupportedLanguages.
//                valueOf(getContext().getRequest().getParameter("languageName"));
        String ip = this.getContext().getRequest().getRemoteAddr();
        documentManager.updateItem(csId, documentType, wsdId, languageName,ip);
        return notClassified();
    }
    
    public Resolution skip(){
        log.debug("Classification AB -> Skip");
//        Long csId = getCsId();
        documentManager.skipItem(csId);
        return notClassified();
    }
    
    public Resolution informatics(){
        log.debug("Classification AB -> Informatics");
        String ip = this.getContext().getRequest().getRemoteAddr();
        documentManager.updateItem(csId, DocumentType.INFORMATICS, wsdId, languageName, ip);
        return notClassified();
    }
    
    public Resolution other(){
        log.debug("Classification AB -> Informatics");
        String ip = this.getContext().getRequest().getRemoteAddr();
        documentManager.updateItem(csId, DocumentType.OTHER, wsdId, languageName, ip);
        return notClassified();
    }
    
    public Resolution unknown(){
        log.debug("Classification AB -> Informatics");
        String ip = this.getContext().getRequest().getRemoteAddr();
        documentManager.updateItem(csId, DocumentType.UNKNOWN, wsdId, languageName, ip);
        return notClassified();
    }
    
    public Resolution informatic_list(){
        log.debug("Classification AB -> informatic_list");
        String ip = this.getContext().getRequest().getRemoteAddr();
        documentManager.updateItem(csId, DocumentType.INFORMATIC_LIST, wsdId, languageName, ip);
        return notClassified();
    }
    
    public Resolution manager(){
        log.debug("Classification AB -> informatic_list");
        String ip = this.getContext().getRequest().getRemoteAddr();
        documentManager.updateItem(csId, DocumentType.MANAGER, wsdId, languageName, ip);
        return notClassified();
    }
    
    public Resolution info_manager(){
        log.debug("Classification AB -> informatic_list");
        String ip = this.getContext().getRequest().getRemoteAddr();
        documentManager.updateItem(csId, DocumentType.INFO_MANAGER, wsdId, languageName, ip);
        return notClassified();
    }
    
    private void loadExtractedText() {
        log.debug("Classification AB -> loadExtractedText");
        if (classificationItem == null) {
            extractedText = "Error: Nothing loaded!";
        }
        try {
            extractedText = FileUtils.readFileToString(
                    new File(classificationItem.getFileLocation()), "UTF-8");
        } catch (IOException ex) {
            log.error("File was not found: " + classificationItem.getFileLocation());
        }
    }
//    
//    private Long getId(String paramName){
//        String ids = getContext().getRequest().getParameter(paramName);
//        Long csId = null;
//        try {
//            csId = Long.parseLong(ids);
//        } catch (NumberFormatException e) {
//            log.error("Parameter '"+paramName+"' was not a number: " + csId);
//        }
//        if (((ids == null) || (csId == null))) {
//            return null;
//        } else {
//            return csId;
//        }
//    }
//
//    private Long getCsId() {
//        return getId("csId");
//    }

    public List<ClassificationItem> getClassificationItems() {
        return classificationItems;
    }

    public void setClassificationItems(List<ClassificationItem> classificationItems) {
        this.classificationItems = classificationItems;
    }

    public ClassificationItem getClassificationItem() {
        return classificationItem;
    }

    public void setClassificationItem(ClassificationItem classificationItem) {
        if(classificationItem != null){
            csId = classificationItem.getId();
            wsdId = classificationItem.getWsdId();
            languageName = classificationItem.getLanguageName();
            documentType = classificationItem.getDocumentType();
        }
        this.classificationItem = classificationItem;
    }

    public DocumentManager getDocumentManager() {
        return documentManager;
    }

    public void setDocumentManager(DocumentManager documentManager) {
        this.documentManager = documentManager;
    }

    public String getExtractedText() {
        return extractedText;
    }

    public void setExtractedText(String extractedText) {
        this.extractedText = extractedText;
    }

    public Long getCsId() {
        return csId;
    }

    public void setCsId(Long csId) {
        this.csId = csId;
    }

    public DocumentType getDocumentType() {
        return documentType;
    }

    public void setDocumentType(DocumentType documentType) {
        this.documentType = documentType;
    }

    public SupportedLanguages getLanguageName() {
        return languageName;
    }

    public void setLanguageName(SupportedLanguages languageName) {
        this.languageName = languageName;
    }

    public Long getWsdId() {
        return wsdId;
    }

    public void setWsdId(Long wsdId) {
        this.wsdId = wsdId;
    }
}
